import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'home',
    component: () => import('@/views/layout'),
    children: [
      { path: 'home', component: () => import('@/views/home') },
      // 运营工单
      { path: 'yunying', component: () => import('@/views/yunyingGd.vue') },
      // 运维工单
      { path: 'yunwei', component: () => import('@/views/yunweiGd .vue') },
      // 区域管理
      { path: 'quyu', component: () => import('@/views/quyuGL.vue') },
      // 点位管理
      { path: 'dianwei', component: () => import('@/views/dianweiGL.vue') },
      // 合作商管理
      {
        path: 'hezuoshang',
        component: () => import('@/views/hezuoshangGL.vue')
      },
      // 设备管理
      { path: 'guanli', component: () => import('@/views/shebeiGL.vue') },
      // 设备状态
      {
        path: 'zhuangtai',
        component: () => import('@/views/shebeizhuangtai.vue')
      },
      // 设备类型管理
      { path: 'leixing', component: () => import('@/views/shebeileixing.vue') },
      // 人员列表
      { path: 'renyuan', component: () => import('@/views/RYliebiao.vue') },
      // 人效统计
      {
        path: 'renxiao',
        component: () => import('@/views/RYrenxiaotongji.vue')
      },
      // 工作量列表
      {
        path: 'gongzuoliang',
        component: () => import('@/views/RYgongzuoliang.vue')
      },
      // 商品类型
      { path: 'spleixing', component: () => import('@/views/SPleixing.vue') },
      // 商品管理
      { path: 'spguanli', component: () => import('@/views/SPlguanli.vue') },
      // 策略管理
      { path: 'celue', component: () => import('@/views/celue.vue') },
      // 订单管理
      { path: 'dingdan', component: () => import('@/views/dingdanguanli.vue') },
      // 对账统计
      {
        path: 'duizhang',
        component: () => import('@/views/duizhangtongji.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '*',
    name: '/404',
    component: () => import('@/views/404.vue')
  }
]

const router = new VueRouter({
  routes
})

const whiteList = ['/login', '404']

router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
